@isTest
private class EZAssignTests {
    
    static List<User> usrList = new List<User>();
    static List<Profile> prflList = [select Id from Profile where name = 'System Administrator' limit 1];
    static List<Group> grpList = new List<Group>();
    static List<GroupMember> gmList = new List<GroupMember>();
    static List<QueueSetting__c> qsList = new List<QueueSetting__c>();
    static List<Case> csList = new List<Case>();
    static List<Lead> ldList = new List<Lead>();
    static List<QueueSobject> qsobList = new List<QueueSobject>();
    static List<QueuePrioritizationRule__c> qprList = new List<QueuePrioritizationRule__c>();
    
    static void createTestData() {
        /** Create Custom settings */
        EZAssignSettings__c assSett = new EZAssignSettings__c();
        assSett.Name = 'Test Sett 1';
        assSett.ObjectAPIName__c = 'Case';
        assSett.ActiveFieldValues__c = 'New';
        assSett.ActiveFieldAPIName__c = 'Status';
        upsert assSett;
        assSett = new EZAssignSettings__c();
        assSett.Name = 'Test Sett 2';
        assSett.ObjectAPIName__c = 'Lead';
        assSett.ActiveFieldValues__c = 'DS';
        assSett.ActiveFieldAPIName__c = 'LastName';
        upsert assSett;
        system.assertNotEquals(assSett, null);
        
        /** Create users */
        User usr = new User();
        usr.LastName = 'Last1';
        usr.Username = 'lst@gmail.com';
        usr.Email = 'lst@gmail.com';
        usr.Alias = 'lst';
        usr.CommunityNickname = 'lst';
        usr.TimeZoneSidKey = 'Africa/Cairo';
        usr.LocaleSidKey = 'en_US';
        usr.EmailEncodingKey = 'ISO-8859-1';
        usr.ProfileId = prflList[0].Id;
        usr.LanguageLocaleKey = 'en_US';
        usr.Workingdays__c = 'Sunday;Monday;Tuesday;Wednesday;Thurthday;Friday;Saturday';
        usrList.add(usr);
        
        usr = new User();
        usr.LastName = 'Last2';
        usr.Username = 'lst2@gmail.com';
        usr.Email = 'lst2@gmail.com';
        usr.Alias = 'lst2';
        usr.CommunityNickname = 'lst2';
        usr.TimeZoneSidKey = 'Africa/Cairo';
        usr.LocaleSidKey = 'en_US';
        usr.EmailEncodingKey = 'ISO-8859-1';
        usr.ProfileId = prflList[0].Id;
        usr.LanguageLocaleKey = 'en_US';
        usr.Workingdays__c = 'Sunday;Monday;Tuesday;Wednesday;Thurthday;Friday;Saturday';
        usrList.add(usr);
        
        insert usrList;
        system.assertNotEquals(usrList, null);
        
        /** Create queue */
        Group grp = new Group();
        grp.Name = 'Group 1';
        grp.Type = 'Queue';
        grpList.add(grp);
        
        grp = new Group();
        grp.Name = 'Group 2';
        grp.Type = 'Queue';
        grpList.add(grp);
        
        insert grpList;
        system.assertNotEquals(grpList, null);
        
        /** Create queue sobject */
        QueueSobject qsob = new QueueSobject();
        qsob.SobjectType = 'Case';
        qsob.QueueId = grpList[0].Id;
        qsobList.add(qsob);
        
        qsob = new QueueSobject();
        qsob.SobjectType = 'Lead';
        qsob.QueueId = grpList[0].Id;
        qsobList.add(qsob);
        
        System.runAs(new User(Id = UserInfo.getUserId(),Workingdays__c = 'Sunday;Monday;Tuesday;Wednesday;Thurthday;Friday;Saturday')) {
            insert qsobList;
            system.assertNotEquals(qsobList, null);
        }
        
        /** Creat queue setting */
        QueueSetting__c qs = new QueueSetting__c();
        qs.Name = 'Qs1';
        qs.AssignmentType__c = 'Round Robin';
        qs.Active__c = true;
        qs.QueueId__c = ((String)grpList[0].Id).substring(0,15);
        qsList.add(qs);
        
        qs = new QueueSetting__c();
        qs.Name = 'Qs1';
        qs.AssignmentType__c = 'Load Balanced';
        qs.Active__c = true;
        qs.QueueId__c = ((String)grpList[1].Id).substring(0,15);
        qsList.add(qs);
        
        System.runAs(new User(Id = UserInfo.getUserId(),Workingdays__c = 'Sunday;Monday;Tuesday;Wednesday;Thurthday;Friday;Saturday')) {
            insert qsList;
            system.assertNotEquals(qsList, null);
        }
        
        /** Create group members */
        GroupMember gm = new GroupMember();
        gm.GroupId = grpList[0].Id;
        gm.UserOrGroupId = usrList[0].Id;
        gmList.add(gm);
        
        gm = new GroupMember();
        gm.GroupId = grpList[0].Id;
        gm.UserOrGroupId = usrList[1].Id;
        gmList.add(gm);
        System.runAs(new User(Id = UserInfo.getUserId(),Workingdays__c = 'Sunday;Monday;Tuesday;Wednesday;Thurthday;Friday;Saturday')) {
            insert gmList;
            system.assertNotEquals(gmList, null);
        }
        
        
        /** Create Priority Rules */
        QueuePrioritizationRule__c qpr = new QueuePrioritizationRule__c();
        qpr.QueueSettingId__c = qsList[0].Id;
        qpr.FieldAPIName__c = 'Status';
        qpr.ExecutionOrder__c = 2;
        qprList.add(qpr);
        
        qpr = new QueuePrioritizationRule__c();
        qpr.QueueSettingId__c = qsList[0].Id;
        qpr.FieldAPIName__c = 'Status';
        qpr.ExecutionOrder__c = 1;
        qprList.add(qpr);
        
        qpr = new QueuePrioritizationRule__c();
        qpr.QueueSettingId__c = qsList[1].Id;
        qpr.FieldAPIName__c = 'Status';
        qpr.ExecutionOrder__c = 1;
        qprList.add(qpr);
        
        insert qprList;
        system.assertNotEquals(qprList, null);
        
        /** Create Case and Leads */
        Lead l = new Lead();
        l.OwnerId = qsobList[1].QueueId;
        l.LastName = 'DS';
        l.Company = 'DS';
        ldList.add(l);
        upsert ldList;
        ldList[0].OwnerId = qsobList[1].QueueId;
        upsert ldList;
        
        Case c = new Case();
        c.OwnerId = ((String)qsobList[0].QueueId).substring(0,15);
        c.Status = 'New';
        c.Origin = 'Phone';
        csList.add(c);
        c = new Case();
        c.OwnerId = ((String)qsobList[0].QueueId).substring(0,15);
        c.Status = 'New';
        c.Origin = 'Phone';
        csList.add(c);
        c = new Case();
        c.OwnerId = ((String)qsobList[0].QueueId).substring(0,15);
        c.Status = 'New';
        c.Origin = 'Phone';
        csList.add(c);
        upsert csList;
        
        c = new Case();
        c.OwnerId = ((String)qsobList[0].QueueId).substring(0,15);
        c.Status = 'New';
        c.Origin = 'Phone';
        csList.add(c);
        upsert csList;
        
        system.assertNotEquals(csList, null);
        system.assertNotEquals(ldList, null);
    }
    
    static testMethod void myUnitTest() {
        createTestData();
        ProcessRecords cntrl = new ProcessRecords(csList,true,false);
        cntrl.processObjects(csList,true);
        system.assertNotEquals(csList, null);
        
        ManualControlController manualController = new ManualControlController();
        manualController.runScheduler();
        system.assertNotEquals(manualController, null);
        
        SchedulerClass sched = new SchedulerClass();
        sched.isTest = true;
        sched.execute(null);
        system.assertNotEquals(sched, null);
        
        ProcessRecords prController = new ProcessRecords(null,null,null);
        prController.start(null);
        prController.execute(null,null);
        prController.finish(null);
        system.assertNotEquals(prController, null);
    }
}